//
//  DYZChooseCarModelController.m
//  发布车源
//
//  Created by xmy on 16/8/19.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "DYZChooseCarModelController.h"
#import "DSHTTPClient.h"
#import "DYZCarsModModel.h"
#import "UIColor+Addition.h"
#import "DYZSearchCarsController.h"


static NSString *cellId = @"cellId";

@interface DYZChooseCarModelController ()
@property(nonatomic,strong)NSArray *dataArray;
@end

@implementation DYZChooseCarModelController

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"%@ ---  %@",self.carId,self.label);
    [self model];
    self.navigationItem.title = @"选择车型";
     self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
}


//型号
- (void)model{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:@"1" forKey:@"accountid"];
    [param setValue:self.carId forKey:@"carLineId"];
    
    [DSHTTPClient postUrlString:@"http://iosapi.itcast.cn/car/model.json.php" withParam:param withSuccessBlock:^(id data) {
       
        NSMutableArray *array = [NSMutableArray array];
        
        NSDictionary *dict = [data objectForKey:@"data"];
        
        NSArray *arr = [dict objectForKey:@"carList"];
        
//        NSLog(@"%@",arr);
        
        for (NSDictionary *dict1 in arr) {
            
            NSArray *arr2 = [dict1 objectForKey:@"modelList"];            
            for (NSDictionary *dict2 in arr2) {
       
                DYZCarsModModel *model = [DYZCarsModModel yy_modelWithDictionary:dict2];
                
                [array addObject:model];
            }
            
            
        }
//        NSLog(@"%@",[array yy_modelDescription]);
        self.dataArray = array;
        [self.tableView reloadData];

        
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
    
    
    
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  
    return self.dataArray.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    
    DYZCarsModModel *model = self.dataArray[indexPath.row];
    
    NSString *str1 = [self.label stringByAppendingFormat:@"%@",model.carModelName];
//    NSLog(@"%@",str1);
    cell.textLabel.text = str1;
    
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    [cell.textLabel setTextColor:[UIColor colorWithHex:0x333333]];
    
    return cell;
}

//cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    //取消点击效果
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
        DYZCarsModModel *model = self.dataArray[indexPath.row];
        NSString *str1 = [self.label stringByAppendingFormat:@"%@",model.carModelName];
    
    //使用通知传值
    NSDictionary *dict = @{@"title":str1};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hello" object:nil userInfo:dict];

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}

@end
